<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">
    <h:head>
        <title>GSE - 350</title>
    </h:head>
    <h:body>

        <br />

        <h:panelGrid style="font-weight: 100; font-size: small" columns="1">

            <h:form>
                
                <p:growl id="ggrowl" autoUpdate="true" life="6000" showDetail="true"/>
                
            </h:form>
            
            
            <h:form>  

                <!-- Leer Datos -->

                <p:panel id="form_main">

                    <h:outputLabel value="GSE - 350" style="font-weight: bold; font-size: x-large; alignment-adjust: central; text-align: center;"/> 

                    <p:commandButton icon="ui-icon-pencil" update=":form_rs232Config" title="Abrir diálogo de configuración RS232"
                                     action="#{managePort.obtainAvailablePorts()}"    
                                     style="width: 25px; height: 25px; font-weight: lighter" 
                                     onclick="dlg_rs232Config.show()"/>

                </p:panel>

            </h:form>



            <p:dialog widgetVar="dlg_rs232Config" header="Diálogo de configuración RS232" 
                      style="font-weight: lighter" dynamic="true"
                      closable="true" draggable="true" showEffect="fade" modal="true">

                <h:form id="form_rs232Config">

                    <p:tabView id="tabView_main">

                        <p:tab title="Configurar puerto">
                            <h:panelGrid id="grid_portConfig" columns="2">
                                <h:outputLabel  value="Puerto seleccionado: " style="font-weight:bold"/>
                                <p:selectOneMenu value="#{managePort.parameters.port}" style="width: 150px"
                                                 disabled="#{false and managePort.validateSerialPortIsOpen(managePort.parameters.port)}">                                    
                                    <f:selectItems value="#{managePort.availableLstPorts}"/> 
                                    <p:ajax listener="#{managePort.printParameters()}" global="true"
                                            update=":form_rs232Config:tabView_main:grid_btnPortConfig, :form_rs232Config:tabView_main:grid_configDevice"/>
                                </p:selectOneMenu>

                                <h:outputLabel  value="Baudios: " style="font-weight:bold"/>
                                <p:selectOneMenu value="#{managePort.strBaud}" style="width: 150px">
                                    <f:selectItems value="#{managePort.availableLstBaud()}" var="_bauds" itemLabel="#{_bauds.comment}" itemValue="#{_bauds.value}"/>                        
                                    <p:ajax listener="#{managePort.printParameters()}"/>
                                </p:selectOneMenu>

                                <h:outputLabel  value="Paridad: " style="font-weight:bold"/>
                                <p:selectOneMenu value="#{managePort.parameters.parity}" style="width: 150px" >
                                    <f:selectItems value="#{managePort.availableLstParity()}" var="_parity" itemLabel="#{_parity.value}, #{_parity.comment}" itemValue="#{_parity.value}"/>                        
                                    <p:ajax listener="#{managePort.printParameters()}"/>
                                </p:selectOneMenu>

                                <h:outputLabel  value="Tamaño de datos: " style="font-weight:bold"/>
                                <h:panelGrid columns="1" style="width: 10px" rendered="false">
                                    <p:spinner value="#{managePort.parameters.byteSize}" min="7" max="8" maxlength="1" suffix=" bits" style="width: fit-content"/>
                                </h:panelGrid>
                                <p:selectOneMenu value="#{managePort.parameters.byteSize}" style="width: 150px" rendered="true" >
                                    <f:selectItems value="#{managePort.availableLstByteSize()}" var="_bsize" itemLabel="#{_bsize.value}" itemValue="#{_bsize.value}"/>                        
                                    <p:ajax listener="#{managePort.printParameters()}"/>
                                </p:selectOneMenu>


                                <h:outputLabel  value="Bits de parada " style="font-weight:bold"/>
                                <h:panelGrid columns="1" style="width: 10px" rendered="false">
                                    <p:spinner value="#{managePort.parameters.stopBits}" min="1" max="2" maxlength="1" suffix=" bits" style="width: fit-content"/>
                                </h:panelGrid>
                                <p:selectOneMenu value="#{managePort.parameters.stopBits}" style="width: 150px" rendered="true" >
                                    <f:selectItems value="#{managePort.availableLstStopBits()}" var="_stop" itemLabel="#{_stop.value}" itemValue="#{_stop.value}"/>                        
                                    <p:ajax listener="#{managePort.printParameters()}"/>
                                </p:selectOneMenu>

                            </h:panelGrid>

                            <p:separator/>

                            <h:panelGrid columns="1">

                                <h:panelGrid id="grid_btnPortConfig" columns="3">

                                    <p:commandButton value="Abrir Puerto" update=":form_rs232Config:tabView_main:grid_portConfig, :form_rs232Config:tabView_main:grid_btnPortConfig" 
                                                     global="true" title="Abrir el puerto serial"
                                                     action="#{managePort.handleOpenPort()}"    
                                                     rendered="#{!managePort.validateSerialPortIsOpen(managePort.parameters.port)}"
                                                     style="font-weight: lighter" 
                                                     />

                                    <p:commandButton value="Cerrar Puerto" update=":form_rs232Config:tabView_main:grid_portConfig, :form_rs232Config:tabView_main:grid_btnPortConfig" 
                                                     global="true" title="Cierra el puerto serial"
                                                     action="#{managePort.handleClosePort()}"
                                                     rendered="#{managePort.validateSerialPortIsOpen(managePort.parameters.port)}"
                                                     style="font-weight: lighter" 
                                                     />

                                    <p:commandButton value="Iniciar Lectura" update=":form_rs232Config" title="Inicia el timer para la lectura cíclica de datos"
                                                     action="#{managePort.startReadingData(1)}"
                                                     rendered="#{true or managePort.validateSerialPortIsOpen(managePort.parameters.port)}"
                                                     style="font-weight: lighter" 
                                                     />

                                    <p:commandButton value="Parar Lectura" update=":form_rs232Config" title="Detiene el timer para la lectura cíclica de datos"
                                                     action="#{managePort.stopReadingData()}"
                                                     rendered="#{true or !managePort.validateSerialPortIsOpen(managePort.parameters.port)}"
                                                     style="font-weight: lighter" 
                                                     />
                                </h:panelGrid>
                            </h:panelGrid>

                        </p:tab>

                        <p:tab title="Configurar dispositivo">                                                        

                            <h:panelGrid id="grid_configDevice" columns="1">

                                <h:panelGrid columns="2">

                                    <h:outputLabel  value="Formato de String de entrada: " style="font-weight: bold"/>
                                    <h:inputText value="#{managePort.bufferFormat}"/>

                                    <h:outputLabel  value="Formato de subString de entrada: " style="font-weight: bold"/>
                                    <h:inputText value="#{managePort.subBuffer}"/>

                                </h:panelGrid>

                                <h:panelGrid columns="3">

                                    <h:outputLabel  value="Ingrese comando a enviar: " style="font-weight: bold"/>

                                    <h:inputText value="#{managePort.commandToSend}"/>

                                    <p:commandButton value="Enviar"  title="Enviar Comando a dispositivo"
                                                     update=":form_rs232Config:tabView_main, :form_rs232Config:tabView_main"
                                                     disabled="#{!managePort.validateSerialPortIsOpen(managePort.parameters.port)}"
                                                     style="width: auto; height: 35px; font-weight: lighter" 
                                                     action="#{managePort.handleWriteData(managePort.commandToSend)}"/> 
                                    <p:commandButton value="AddEnter" rendered="false"
                                                     update=":form_rs232Config:tabView_main:grid_configDevice" title="Enviar Comando a dispositivo"
                                                     style="width: auto; height: 35px; font-weight: lighter" 
                                                     action="#{managePort.handleWriteData(managePort.commandToSend)}"/>                                    

                                </h:panelGrid>

                                <h:panelGrid id="grid_data" columns="2">

                                    <h:outputLabel  value="Valor (kg): " style="font-weight: bold"/>
                                    <h:outputLabel value="#{managePort.data}"/>
                                    <p:progressBar value="#{managePort.data/2}" labelTemplate="KG" />

                                </h:panelGrid>

                            </h:panelGrid>
                        </p:tab>    

                    </p:tabView>


                </h:form>

            </p:dialog>

        </h:panelGrid>

        <br/>
        <h:link outcome="welcomePrimefaces" value="Primefaces welcome page" />
    </h:body>
</html>

